def lazy_sum(*args):
    def sum():
        ax = 0
        for n in args:
            ax +=n
        return ax
    return sum
L1 = [1,23,2,32,3,21,3]
f = lazy_sum(*L1)
print(f())


# 实际全是9，并不是1，4，9
# def count():
#     fs = []
#     for i in range(1,4):
#         def f():
#             return i * i
#         fs.append(f)
#     return fs

#  改进版
def count():
    fs = []
    for i in range(1, 4):
        fs.append(lambda i = i:i*i)
    return fs
f1, f2, f3 = count()

ret = count();
for func in ret:
    print(func())

# 内层函数f中直接使用x，报错：x变量不存在。
# def inc():
#     x = 0
#     def f():
#         x = x +1
#         return x
#     return f


# 改进版
def inc():
    x = 0
    def f():
        nonlocal x # 必须先声明再使用，声明的时候不能初始化
        x = x+1
        return x
    return f
print(inc()())

print("======================================================practice")
def createCounter():
    num = 0
    def counter():
        nonlocal num 
        num += 1
        return num
    return counter

# 测试:
counterA = createCounter()
print(counterA(), counterA(), counterA(), counterA(), counterA()) # 1 2 3 4 5
counterB = createCounter()
if [counterB(), counterB(), counterB(), counterB()] == [1, 2, 3, 4]:
    print('测试通过!')
else:
    print('测试失败!')

    

